package com.michaelbulava.TFS;

import com.intellij.CommonBundle;
import com.intellij.openapi.project.Project;
import com.michaelbulava.TFS.ui.helpers.SystemIconHelper;
import com.michaelbulava.TFS.ui.helpers.UIHelpers;
import com.microsoft.tfs.util.Check;
import org.jetbrains.annotations.PropertyKey;

import javax.swing.*;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Created by mbulava on 5/6/2015.
 */
public class Messages {
    private static Reference<ResourceBundle> ourBundle;
    private static final String BUNDLE = "com.michaelbulava.TFS.Messages";

    public static final int YES = com.intellij.openapi.ui.Messages.YES;
    public static final int NO = com.intellij.openapi.ui.Messages.NO;
    public static final int OK = com.intellij.openapi.ui.Messages.OK;
    public static final int CANCEL = com.intellij.openapi.ui.Messages.CANCEL;

    public static String message(@PropertyKey(resourceBundle=BUNDLE) String key, Object... params){
        Check.notNull(key, "key");
        Check.notNull(params, "params");
        return CommonBundle.message(getBundle(), key, params);
    }

    private static ResourceBundle getBundle(){
        ResourceBundle bundle = (ResourceBundle)com.intellij.reference.SoftReference.dereference(ourBundle);
        if (bundle == null){
            bundle = ResourceBundle.getBundle(BUNDLE, Locale.getDefault());
            ourBundle = new SoftReference<ResourceBundle>(bundle);
        }
        return bundle;
    }

    public static String getString(String key) {
        return getBundle().getString(key);
    }

    public static String getString(String key, Locale locale){
        return getString(key);
    }

    public static void showErrorDialog(final JComponent owner, final String message, final String title){
        UIHelpers.runOnUIThread(false, new Runnable() {
            @Override
            public void run() {
                com.intellij.openapi.ui.Messages.showErrorDialog(owner, message, title);
            }
        });
    }

    public static void showErrorDialog(final Project project, final String message, final String title){
        UIHelpers.runOnUIThread(false, new Runnable() {
            @Override
            public void run() {
                com.intellij.openapi.ui.Messages.showErrorDialog(project, message, title);
            }
        });
    }

    public static void showErrorDialog(final String message, final String title){
        UIHelpers.runOnUIThread(false, new Runnable() {
            @Override
            public void run() {
                com.intellij.openapi.ui.Messages.showErrorDialog(message, title);
            }
        });
    }

    public static void showInfoMessage(final JComponent owner, final String message, final String title){
        UIHelpers.runOnUIThread(false, new Runnable() {
            @Override
            public void run() {
                com.intellij.openapi.ui.Messages.showInfoMessage(owner, message, title);
            }
        });
    }

    public static void showInfoMessage(final Project project, final String message, final String title){
        UIHelpers.runOnUIThread(false, new Runnable() {
            @Override
            public void run() {
                com.intellij.openapi.ui.Messages.showInfoMessage(project, message, title);
            }
        });
    }

    public static void showInfoMessage(final String message, final String title){
        UIHelpers.runOnUIThread(false, new Runnable() {
            @Override
            public void run() {
                com.intellij.openapi.ui.Messages.showErrorDialog(message, title);
            }
        });
    }


    public static int showYesNoDialog(JComponent owner, String message, String title){
        return com.intellij.openapi.ui.Messages.showYesNoDialog(owner, message, title, SystemIconHelper.getQuestionIcon());
    }

    public static int showYesNoDialog(Project project, String message, String title){
        return com.intellij.openapi.ui.Messages.showYesNoDialog(project, message, title, SystemIconHelper.getQuestionIcon());
    }

    public static int showYesNoDialog(String message, String title){
        return com.intellij.openapi.ui.Messages.showYesNoDialog(message, title, SystemIconHelper.getQuestionIcon());
    }
}
